package com.bjgov.scoresys.action;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

import com.bjgov.scoresys.entity.Project;
import com.bjgov.scoresys.entity.ProjectItems;
import com.bjgov.scoresys.entity.ScoreHistory;
import com.bjgov.scoresys.entity.User;
import com.bjgov.scoresys.entity.UserProject;
import com.bjgov.scoresys.service.ProjectHandler;
import com.bjgov.scoresys.service.ProjectItemsHandler;
import com.bjgov.scoresys.service.ScoreHistoryHandler;
import com.bjgov.scoresys.service.UserProjectHandler;
import com.bjgov.scoresys.util.RequestUtil;

public class ScoreHistoryAction extends MappingDispatchAction{
	
	public ActionForward saveScore(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		int treeId = RequestUtil.getIntParameter(request, "treeId", 0);
		int projectId = RequestUtil.getIntParameter(request, "projectId", 0);
		Project p = ProjectHandler.getProject(projectId);
		//未到评分时间或项目评分已结束
		if(p.getStatus()==0 || p.getStartTime().getTime()>Calendar.getInstance().getTimeInMillis()) {
			request.setAttribute("message", "项目未创建完成或评分时间未到");
			request.setAttribute("returnURL", mapping.getInput() + "?projectId=" + projectId + "&treeId=" + treeId);
			return mapping.findForward("fail");		
		}
		if(p.getStatus()==2) {
			request.setAttribute("message", "项目评分已结束");
			request.setAttribute("returnURL", mapping.getInput() + "?projectId=" + projectId + "&treeId=" + treeId);
			return mapping.findForward("fail");		
		}		
		User user = (User) request.getSession().getAttribute("user");
		List<ProjectItems> projectItemsList = ProjectItemsHandler.getByProjectIdTreeId(projectId, treeId);
		for(int i=0; i<projectItemsList.size(); i++) {
			ProjectItems item = projectItemsList.get(i);
			ScoreHistory history = ScoreHistoryHandler.getByUidPidPitemId(user.getId(),projectId,item.getScoreItem().getId());
			if(history==null) {
				history = new ScoreHistory();
				history.setProjectId(projectId);
				history.setProjectItemId(item.getScoreItem().getId());
				history.setStandardScore(item.getScore());
				history.setTopCateId(item.getTopCateId());
				history.setUser(user);				
			}
			history.setDecScore(RequestUtil.getFloatParameter(request, "decScore_" + item.getScoreItem().getId(), 0));
			ScoreHistoryHandler.save(history);
		}
		
		UserProject up = UserProjectHandler.getUserProjectByUidPid(user.getId(), projectId);
		up.setStatus((short)1);//标记用户正在评分中
		UserProjectHandler.save(up);
		
		request.setAttribute("message", "评分成功");
		request.setAttribute("nextURL", mapping.getInput() + "?projectId=" + projectId + "&treeId=" + treeId);
		return mapping.findForward("tip");
	}
	
	public ActionForward completeScore(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		int projectId = RequestUtil.getIntParameter(request, "projectId", 0);
		Project p = ProjectHandler.getProject(projectId);
		//未到评分时间或项目评分已结束
		if(p.getStatus()==0 || p.getStartTime().getTime()>Calendar.getInstance().getTimeInMillis()) {
			request.setAttribute("message", "项目未创建完成或评分时间未到");
			request.setAttribute("returnURL", mapping.getInput());
			return mapping.findForward("fail");		
		}
		if(p.getStatus()==2) {
			request.setAttribute("message", "项目评分已结束");
			request.setAttribute("returnURL", mapping.getInput());
			return mapping.findForward("fail");		
		}		
		User user = (User) request.getSession().getAttribute("user");
		UserProject up = UserProjectHandler.getUserProjectByUidPid(user.getId(), projectId);
		//找出用户评分详情，计算用户的总分
	    float buildUnitScore = 0.0f;//有效建设单位评分总和
	    float constructUnitScore = 0.0f;//有效施工单位评分总和
	    float supervisorUnitScore = 0.0f;//有效监理单位评分总和
	    float designUnitScore = 0.0f;//有效设计单位评分总和
	    
		List<ScoreHistory> scoreList = ScoreHistoryHandler.getByUserIdProjectId(user.getId(), projectId);
		
		Map<Integer,String> scoreMap = new HashMap<Integer,String>();
		for(int i=0; i<scoreList.size(); i++) {
			scoreMap.put(scoreList.get(i).getProjectItemId(), "");
		}
		Set<ProjectItems> allProjectItems = p.getProjectItemses();
		if(allProjectItems.size()!=scoreList.size()) {
			for(ProjectItems item: allProjectItems) {
				if(!scoreMap.containsKey(item.getScoreItem().getId())) {
					//项目评分不完全，填充未评分项
					ScoreHistory history = new ScoreHistory();
					history.setProjectId(projectId);
					history.setProjectItemId(item.getScoreItem().getId());
					history.setStandardScore(item.getScore());
					history.setTopCateId(item.getTopCateId());
					history.setUser(user);		
					history.setDecScore(0.0f);
					ScoreHistoryHandler.save(history);
				}
			}
		}
		
		scoreList = ScoreHistoryHandler.getByUserIdProjectId(user.getId(), projectId);
		
		for(ScoreHistory history: scoreList) {
			if(history.getTopCateId()==1) {//建设单位
				buildUnitScore += (history.getStandardScore()-history.getDecScore());
			}
			if(history.getTopCateId()==3) {//施工单位
				constructUnitScore += (history.getStandardScore()-history.getDecScore());
			}
			if(history.getTopCateId()==40) {//监理单位
				supervisorUnitScore += (history.getStandardScore()-history.getDecScore());
			}
			if(history.getTopCateId()==44) {//设计单位
				designUnitScore += (history.getStandardScore()-history.getDecScore());
			}
		}
		up.setBuildUnitScore(buildUnitScore);
	    up.setConstructUnitScore(constructUnitScore);
	    up.setSupervisorUnitScore(supervisorUnitScore);
	    up.setDesignUnitScore(designUnitScore);
		up.setStatus((short)2);//标记项目评分完成
		up.setCompletetime(new Date());
		UserProjectHandler.save(up);
		request.setAttribute("message", "对本项目的评分完成!");
		request.setAttribute("nextURL", "/judge/nowScoreProject.jsp");
		return mapping.findForward("tip");
	}	
}
